package com.agile4j.oss;

import com.agile4j.oss.core.OssProperties;
import com.agile4j.oss.server.OssTemplate;
import io.minio.MinioClient;
import lombok.RequiredArgsConstructor;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import software.amazon.awssdk.auth.credentials.AwsBasicCredentials;
import software.amazon.awssdk.auth.credentials.AwsCredentialsProvider;
import software.amazon.awssdk.auth.credentials.DefaultCredentialsProvider;
import software.amazon.awssdk.auth.credentials.StaticCredentialsProvider;
import software.amazon.awssdk.core.client.config.ClientOverrideConfiguration;
import software.amazon.awssdk.http.apache.ApacheHttpClient;
import software.amazon.awssdk.regions.Region;
import software.amazon.awssdk.services.s3.S3Client;
import software.amazon.awssdk.services.s3.S3Configuration;
import software.amazon.awssdk.services.s3.presigner.S3Presigner;

import java.net.URI;

/**
 * @Author JiaQIng
 * @Description oss配置bean
 * @ClassName OssAConfiguration
 * @Date 2023/3/18 18:23
 **/
@Configuration(proxyBeanMethods = false)
@RequiredArgsConstructor
@EnableConfigurationProperties(OssProperties.class)
public class OssAutoConfiguration {

//    @Bean
//    @ConditionalOnMissingBean
//    public AmazonS3 ossClient(OssProperties ossProperties) {
//        // 客户端配置，主要是全局的配置信息
//        ClientConfiguration clientConfiguration = new ClientConfiguration();
//        clientConfiguration.setMaxConnections(ossProperties.getMaxConnections());
//        // url以及region配置
//        AwsClientBuilder.EndpointConfiguration endpointConfiguration = new AwsClientBuilder.EndpointConfiguration(
//                ossProperties.getEndpoint(), ossProperties.getRegion());
//        // 凭证配置
//        AWSCredentials awsCredentials = new BasicAWSCredentials(ossProperties.getAccessKey(),
//                ossProperties.getSecretKey());
//        AWSCredentialsProvider awsCredentialsProvider = new AWSStaticCredentialsProvider(awsCredentials);
//        // build amazonS3Client客户端
//        return AmazonS3Client.builder().withEndpointConfiguration(endpointConfiguration)
//                .withClientConfiguration(clientConfiguration).withCredentials(awsCredentialsProvider)
//                .disableChunkedEncoding().withPathStyleAccessEnabled(ossProperties.getPathStyleAccess()).build();
//    }
@Bean
@ConditionalOnMissingBean
public S3Client s3Client(OssProperties ossProperties) {

    // 1. 创建凭证
    AwsBasicCredentials credentials = AwsBasicCredentials.create(
            ossProperties.getAccessKey(),
            ossProperties.getSecretKey()
    );

//    // 2. 自定义 HTTP 客户端（替代 ClientConfiguration）
//    ApacheHttpClient httpClient = ApacheHttpClient.builder()
//            .maxConnections(ossProperties.getMaxConnections())
//            .build();

    // 3. S3 特定配置（替代 withPathStyleAccessEnabled）
    S3Configuration s3Config = S3Configuration.builder()
            .pathStyleAccessEnabled(ossProperties.getPathStyleAccess())
            .build();

    // 4. 构建 S3Client
    return S3Client.builder()
            .httpClient(ApacheHttpClient.builder().maxConnections(200).build())
            // 使用 Region.of() 支持自定义区域名
            .region(Region.of(ossProperties.getRegion()))
            .credentialsProvider(StaticCredentialsProvider.create(credentials))
            // 替代 withEndpointConfiguration
            .endpointOverride(URI.create(ossProperties.getEndpoint()))
            .serviceConfiguration(s3Config)
            // 可自定义重试策略
//            .overrideConfiguration(ClientOverrideConfiguration.builder()
//                    .retryPolicy(RetryPolicy.defaultRetryPolicy())
//                    .build())
            .overrideConfiguration(ClientOverrideConfiguration.builder().build())
                    // 不设置 retryPolicy → 使用 SDK 默认重试策略
            .build();
}


    @Bean
    @ConditionalOnMissingBean
    public MinioClient minioClient(OssProperties ossProperties) {
        // 客户端配置，主要是全局的配置信息
//        ClientConfiguration clientConfiguration = new ClientConfiguration();
//        clientConfiguration.setMaxConnections(ossProperties.getMaxConnections());
        MinioClient.Builder builder=new MinioClient.Builder();
        builder.credentials(ossProperties.getAccessKey(),
                ossProperties.getSecretKey());
        builder.endpoint(ossProperties.getEndpoint());
        builder.region(ossProperties.getRegion());
        return builder.build();
//        // url以及region配置
//        AwsClientBuilder.EndpointConfiguration endpointConfiguration = new AwsClientBuilder.EndpointConfiguration(
//                ossProperties.getEndpoint(), ossProperties.getRegion());
//        // 凭证配置
//        AWSCredentials awsCredentials = new BasicAWSCredentials(ossProperties.getAccessKey(),
//                ossProperties.getSecretKey());
//        AWSCredentialsProvider awsCredentialsProvider = new AWSStaticCredentialsProvider(awsCredentials);
//        // build amazonS3Client客户端
//        return AmazonS3Client.builder().withEndpointConfiguration(endpointConfiguration)
//                .withClientConfiguration(clientConfiguration).withCredentials(awsCredentialsProvider)
//                .disableChunkedEncoding().withPathStyleAccessEnabled(ossProperties.getPathStyleAccess()).build();
    }

    @Bean
    public AwsCredentialsProvider awsCredentialsProvider() {
        return DefaultCredentialsProvider.create();
    }


    @Bean
    public Region awsRegion(OssProperties ossProperties) {
        return Region.of(ossProperties.getRegion());
    }


    // ✅ 关键：添加 S3Presigner Bean
    @Bean
    public S3Presigner s3Presigner(AwsCredentialsProvider credentialsProvider, Region region) {
        return S3Presigner.builder()
                .credentialsProvider(credentialsProvider)
                .region(region)
                .build();
    }
//
//    @Bean
//    public OssTemplate ossTemplate(){
//        return new OssTemplate();
//    }
}
